home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / unix / flex_2_3 / part11 < prev    next >
Encoding:
Internet Message Format  |  1990-08-19  |  68.3 KB

  1. Path: abcfd20.larc.nasa.gov!amiga-request
  2. From: amiga-request@abcfd20.larc.nasa.gov (Amiga Sources/Binaries Moderator)
  3. Subject: v90i238: flex 2.3 - fast lexical analyzer generator, Part11/13
  4. Reply-To: loftus@wpllabs.uucp (William P Loftus)
  5. Newsgroups: comp.sources.amiga
  6. Message-ID: <comp.sources.amiga:v90i238@abcfd20.larc.nasa.gov>
  7. Date: 19 Aug 90 22:43:21 GMT
  8. Approved: tadguy@uunet.UU.NET (Tad Guy)
  9. X-Mail-Submissions-To: amiga@uunet.uu.net
  10. X-Post-Discussions-To: comp.sys.amiga
  11.  
  12. Submitted-by: loftus@wpllabs.uucp (William P Loftus)
  13. Posting-number: Volume 90, Issue 238
  14. Archive-name: unix/flex-2.3/part11
  15.  
  16. #!/bin/sh
  17. # This is a shell archive.  Remove anything before this line, then unpack
  18. # it by saving it into a file and typing "sh file".  To overwrite existing
  19. # files, type "sh file -c".  You can also feed this as standard input via
  20. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  21. # will see the following message at the end:
  22. #        "End of archive 11 (of 13)."
  23. # Contents:  flexdoc.1
  24. # Wrapped by tadguy@abcfd20 on Sun Aug 19 18:41:49 1990
  25. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  26. if test -f 'flexdoc.1' -a "${1}" != "-c" ; then 
  27.   echo shar: Will not clobber existing file \"'flexdoc.1'\"
  28. else
  29. echo shar: Extracting \"'flexdoc.1'\" \(65353 characters\)
  30. sed "s/^X//" >'flexdoc.1' <<'END_OF_FILE'
  31. X.TH FLEX 1 "26 May 1990" "Version 2.3"
  32. X.SH NAME
  33. Xflex - fast lexical analyzer generator
  34. X.SH SYNOPSIS
  35. X.B flex
  36. X.B [-bcdfinpstvFILT8 -C[efmF] -Sskeleton]
  37. X.I [filename ...]
  38. X.SH DESCRIPTION
  39. X.I flex
  40. Xis a tool for generating
  41. X.I scanners:
  42. Xprograms which recognized lexical patterns in text.
  43. X.I flex
  44. Xreads
  45. Xthe given input files, or its standard input if no file names are given,
  46. Xfor a description of a scanner to generate.  The description is in
  47. Xthe form of pairs
  48. Xof regular expressions and C code, called
  49. X.I rules.  flex
  50. Xgenerates as output a C source file,
  51. X.B lex.yy.c,
  52. Xwhich defines a routine
  53. X.B yylex().
  54. XThis file is compiled and linked with the
  55. X.B -lfl
  56. Xlibrary to produce an executable.  When the executable is run,
  57. Xit analyzes its input for occurrences
  58. Xof the regular expressions.  Whenever it finds one, it executes
  59. Xthe corresponding C code.
  60. X.SH SOME SIMPLE EXAMPLES
  61. X.LP
  62. XFirst some simple examples to get the flavor of how one uses
  63. X.I flex.
  64. XThe following
  65. X.I flex
  66. Xinput specifies a scanner which whenever it encounters the string
  67. X"username" will replace it with the user's login name:
  68. X.nf
  69. X
  70. X    %%
  71. X    username    printf( "%s", getlogin() );
  72. X
  73. X.fi
  74. XBy default, any text not matched by a
  75. X.I flex
  76. Xscanner
  77. Xis copied to the output, so the net effect of this scanner is
  78. Xto copy its input file to its output with each occurrence
  79. Xof "username" expanded.
  80. XIn this input, there is just one rule.  "username" is the
  81. X.I pattern
  82. Xand the "printf" is the
  83. X.I action.
  84. XThe "%%" marks the beginning of the rules.
  85. X.LP
  86. XHere's another simple example:
  87. X.nf
  88. X
  89. X        int num_lines = 0, num_chars = 0;
  90. X
  91. X    %%
  92. X    \\n    ++num_lines; ++num_chars;
  93. X    .     ++num_chars;
  94. X
  95. X    %%
  96. X    main()
  97. X        {
  98. X        yylex();
  99. X        printf( "# of lines = %d, # of chars = %d\\n",
  100. X                num_lines, num_chars );
  101. X        }
  102. X
  103. X.fi
  104. XThis scanner counts the number of characters and the number
  105. Xof lines in its input (it produces no output other than the
  106. Xfinal report on the counts).  The first line
  107. Xdeclares two globals, "num_lines" and "num_chars", which are accessible
  108. Xboth inside
  109. X.B yylex()
  110. Xand in the
  111. X.B main()
  112. Xroutine declared after the second "%%".  There are two rules, one
  113. Xwhich matches a newline ("\\n") and increments both the line count and
  114. Xthe character count, and one which matches any character other than
  115. Xa newline (indicated by the "." regular expression).
  116. X.LP
  117. XA somewhat more complicated example:
  118. X.nf
  119. X
  120. X    /* scanner for a toy Pascal-like language */
  121. X
  122. X    %{
  123. X    /* need this for the call to atof() below */
  124. X    #include <math.h>
  125. X    %}
  126. X
  127. X    DIGIT    [0-9]
  128. X    ID       [a-z][a-z0-9]*
  129. X
  130. X    %%
  131. X
  132. X    {DIGIT}+    {
  133. X                printf( "An integer: %s (%d)\\n", yytext,
  134. X                        atoi( yytext ) );
  135. X                }
  136. X
  137. X    {DIGIT}+"."{DIGIT}*        {
  138. X                printf( "A float: %s (%g)\\n", yytext,
  139. X                        atof( yytext ) );
  140. X                }
  141. X
  142. X    if|then|begin|end|procedure|function        {
  143. X                printf( "A keyword: %s\\n", yytext );
  144. X                }
  145. X
  146. X    {ID}        printf( "An identifier: %s\\n", yytext );
  147. X
  148. X    "+"|"-"|"*"|"/"   printf( "An operator: %s\\n", yytext );
  149. X
  150. X    "{"[^}\\n]*"}"     /* eat up one-line comments */
  151. X
  152. X    [ \\t\\n]+          /* eat up whitespace */
  153. X
  154. X    .           printf( "Unrecognized character: %s\\n", yytext );
  155. X
  156. X    %%
  157. X
  158. X    main( argc, argv )
  159. X    int argc;
  160. X    char **argv;
  161. X        {
  162. X        ++argv, --argc;  /* skip over program name */
  163. X        if ( argc > 0 )
  164. X                yyin = fopen( argv[0], "r" );
  165. X        else
  166. X                yyin = stdin;
  167. X        
  168. X        yylex();
  169. X        }
  170. X
  171. X.fi
  172. XThis is the beginnings of a simple scanner for a language like
  173. XPascal.  It identifies different types of
  174. X.I tokens
  175. Xand reports on what it has seen.
  176. X.LP
  177. XThe details of this example will be explained in the following
  178. Xsections.
  179. X.SH FORMAT OF THE INPUT FILE
  180. XThe
  181. X.I flex
  182. Xinput file consists of three sections, separated by a line with just
  183. X.B %%
  184. Xin it:
  185. X.nf
  186. X
  187. X    definitions
  188. X    %%
  189. X    rules
  190. X    %%
  191. X    user code
  192. X
  193. X.fi
  194. XThe
  195. X.I definitions
  196. Xsection contains declarations of simple
  197. X.I name
  198. Xdefinitions to simplify the scanner specification, and declarations of
  199. X.I start conditions,
  200. Xwhich are explained in a later section.
  201. X.LP
  202. XName definitions have the form:
  203. X.nf
  204. X
  205. X    name definition
  206. X
  207. X.fi
  208. XThe "name" is a word beginning with a letter or an underscore ('_')
  209. Xfollowed by zero or more letters, digits, '_', or '-' (dash).
  210. XThe definition is taken to begin at the first non-white-space character
  211. Xfollowing the name and continuing to the end of the line.
  212. XThe definition can subsequently be referred to using "{name}", which
  213. Xwill expand to "(definition)".  For example,
  214. X.nf
  215. X
  216. X    DIGIT    [0-9]
  217. X    ID       [a-z][a-z0-9]*
  218. X
  219. X.fi
  220. Xdefines "DIGIT" to be a regular expression which matches a
  221. Xsingle digit, and
  222. X"ID" to be a regular expression which matches a letter
  223. Xfollowed by zero-or-more letters-or-digits.
  224. XA subsequent reference to
  225. X.nf
  226. X
  227. X    {DIGIT}+"."{DIGIT}*
  228. X
  229. X.fi
  230. Xis identical to
  231. X.nf
  232. X
  233. X    ([0-9])+"."([0-9])*
  234. X
  235. X.fi
  236. Xand matches one-or-more digits followed by a '.' followed
  237. Xby zero-or-more digits.
  238. X.LP
  239. XThe
  240. X.I rules
  241. Xsection of the
  242. X.I flex
  243. Xinput contains a series of rules of the form:
  244. X.nf
  245. X
  246. X    pattern   action
  247. X
  248. X.fi
  249. Xwhere the pattern must be unindented and the action must begin
  250. Xon the same line.
  251. X.LP
  252. XSee below for a further description of patterns and actions.
  253. X.LP
  254. XFinally, the user code section is simply copied to
  255. X.B lex.yy.c
  256. Xverbatim.
  257. XIt is used for companion routines which call or are called
  258. Xby the scanner.  The presence of this section is optional;
  259. Xif it is missing, the second
  260. X.B %%
  261. Xin the input file may be skipped, too.
  262. X.LP
  263. XIn the definitions and rules sections, any
  264. X.I indented
  265. Xtext or text enclosed in
  266. X.B %{
  267. Xand
  268. X.B %}
  269. Xis copied verbatim to the output (with the %{}'s removed).
  270. XThe %{}'s must appear unindented on lines by themselves.
  271. X.LP
  272. XIn the rules section,
  273. Xany indented or %{} text appearing before the
  274. Xfirst rule may be used to declare variables
  275. Xwhich are local to the scanning routine and (after the declarations)
  276. Xcode which is to be executed whenever the scanning routine is entered.
  277. XOther indented or %{} text in the rule section is still copied to the output,
  278. Xbut its meaning is not well-defined and it may well cause compile-time
  279. Xerrors (this feature is present for
  280. X.I POSIX
  281. Xcompliance; see below for other such features).
  282. X.LP
  283. XIn the definitions section, an unindented comment (i.e., a line
  284. Xbeginning with "/*") is also copied verbatim to the output up
  285. Xto the next "*/".  Also, any line in the definitions section
  286. Xbeginning with '#' is ignored, though this style of comment is
  287. Xdeprecated and may go away in the future.
  288. X.SH PATTERNS
  289. XThe patterns in the input are written using an extended set of regular
  290. Xexpressions.  These are:
  291. X.nf
  292. X
  293. X    x          match the character 'x'
  294. X    .          any character except newline
  295. X    [xyz]      a "character class"; in this case, the pattern
  296. X                 matches either an 'x', a 'y', or a 'z'
  297. X    [abj-oZ]   a "character class" with a range in it; matches
  298. X                 an 'a', a 'b', any letter from 'j' through 'o',
  299. X                 or a 'Z'
  300. X    [^A-Z]     a "negated character class", i.e., any character
  301. X                 but those in the class.  In this case, any
  302. X                 character EXCEPT an uppercase letter.
  303. X    [^A-Z\\n]   any character EXCEPT an uppercase letter or
  304. X                 a newline
  305. X    r*         zero or more r's, where r is any regular expression
  306. X    r+         one or more r's
  307. X    r?         zero or one r's (that is, "an optional r")
  308. X    r{2,5}     anywhere from two to five r's
  309. X    r{2,}      two or more r's
  310. X    r{4}       exactly 4 r's
  311. X    {name}     the expansion of the "name" definition
  312. X               (see above)
  313. X    "[xyz]\\"foo"
  314. X               the literal string: [xyz]"foo
  315. X    \\X         if X is an 'a', 'b', 'f', 'n', 'r', 't', or 'v',
  316. X                 then the ANSI-C interpretation of \\x.
  317. X                 Otherwise, a literal 'X' (used to escape
  318. X                 operators such as '*')
  319. X    \\123       the character with octal value 123
  320. X    \\x2a       the character with hexadecimal value 2a
  321. X    (r)        match an r; parentheses are used to override
  322. X                 precedence (see below)
  323. X
  324. X
  325. X    rs         the regular expression r followed by the
  326. X                 regular expression s; called "concatenation"
  327. X
  328. X
  329. X    r|s        either an r or an s
  330. X
  331. X
  332. X    r/s        an r but only if it is followed by an s.  The
  333. X                 s is not part of the matched text.  This type
  334. X                 of pattern is called as "trailing context".
  335. X    ^r         an r, but only at the beginning of a line
  336. X    r$         an r, but only at the end of a line.  Equivalent
  337. X                 to "r/\\n".
  338. X
  339. X
  340. X    <s>r       an r, but only in start condition s (see
  341. X               below for discussion of start conditions)
  342. X    <s1,s2,s3>r
  343. X               same, but in any of start conditions s1,
  344. X               s2, or s3
  345. X
  346. X
  347. X    <<EOF>>    an end-of-file
  348. X    <s1,s2><<EOF>>
  349. X               an end-of-file when in start condition s1 or s2
  350. X
  351. X.fi
  352. XThe regular expressions listed above are grouped according to
  353. Xprecedence, from highest precedence at the top to lowest at the bottom.
  354. XThose grouped together have equal precedence.  For example,
  355. X.nf
  356. X
  357. X    foo|bar*
  358. X
  359. X.fi
  360. Xis the same as
  361. X.nf
  362. X
  363. X    (foo)|(ba(r*))
  364. X
  365. X.fi
  366. Xsince the '*' operator has higher precedence than concatenation,
  367. Xand concatenation higher than alternation ('|').  This pattern
  368. Xtherefore matches
  369. X.I either
  370. Xthe string "foo"
  371. X.I or
  372. Xthe string "ba" followed by zero-or-more r's.
  373. XTo match "foo" or zero-or-more "bar"'s, use:
  374. X.nf
  375. X
  376. X    foo|(bar)*
  377. X
  378. X.fi
  379. Xand to match zero-or-more "foo"'s-or-"bar"'s:
  380. X.nf
  381. X
  382. X    (foo|bar)*
  383. X
  384. X.fi
  385. X.LP
  386. XSome notes on patterns:
  387. X.IP -
  388. XA negated character class such as the example "[^A-Z]"
  389. Xabove
  390. X.I will match a newline
  391. Xunless "\\n" (or an equivalent escape sequence) is one of the
  392. Xcharacters explicitly present in the negated character class
  393. X(e.g., "[^A-Z\\n]").  This is unlike how many other regular
  394. Xexpression tools treat negated character classes, but unfortunately
  395. Xthe inconsistency is historically entrenched.
  396. XMatching newlines means that a pattern like [^"]* can match an entire
  397. Xinput (overflowing the scanner's input buffer) unless there's another
  398. Xquote in the input.
  399. X.IP -
  400. XA rule can have at most one instance of trailing context (the '/' operator
  401. Xor the '$' operator).  The start condition, '^', and "<<EOF>>" patterns
  402. Xcan only occur at the beginning of a pattern, and, as well as with '/' and '$',
  403. Xcannot be grouped inside parentheses.  A '^' which does not occur at
  404. Xthe beginning of a rule or a '$' which does not occur at the end of
  405. Xa rule loses its special properties and is treated as a normal character.
  406. X.IP
  407. XThe following are illegal:
  408. X.nf
  409. X
  410. X    foo/bar$
  411. X    <sc1>foo<sc2>bar
  412. X
  413. X.fi
  414. XNote that the first of these, can be written "foo/bar\\n".
  415. X.IP
  416. XThe following will result in '$' or '^' being treated as a normal character:
  417. X.nf
  418. X
  419. X    foo|(bar$)
  420. X    foo|^bar
  421. X
  422. X.fi
  423. XIf what's wanted is a "foo" or a bar-followed-by-a-newline, the following
  424. Xcould be used (the special '|' action is explained below):
  425. X.nf
  426. X
  427. X    foo      |
  428. X    bar$     /* action goes here */
  429. X
  430. X.fi
  431. XA similar trick will work for matching a foo or a
  432. Xbar-at-the-beginning-of-a-line.
  433. X.SH HOW THE INPUT IS MATCHED
  434. XWhen the generated scanner is run, it analyzes its input looking
  435. Xfor strings which match any of its patterns.  If it finds more than
  436. Xone match, it takes the one matching the most text (for trailing
  437. Xcontext rules, this includes the length of the trailing part, even
  438. Xthough it will then be returned to the input).  If it finds two
  439. Xor more matches of the same length, the
  440. Xrule listed first in the
  441. X.I flex
  442. Xinput file is chosen.
  443. X.LP
  444. XOnce the match is determined, the text corresponding to the match
  445. X(called the
  446. X.I token)
  447. Xis made available in the global character pointer
  448. X.B yytext,
  449. Xand its length in the global integer
  450. X.B yyleng.
  451. XThe
  452. X.I action
  453. Xcorresponding to the matched pattern is then executed (a more
  454. Xdetailed description of actions follows), and then the remaining
  455. Xinput is scanned for another match.
  456. X.LP
  457. XIf no match is found, then the
  458. X.I default rule
  459. Xis executed: the next character in the input is considered matched and
  460. Xcopied to the standard output.  Thus, the simplest legal
  461. X.I flex
  462. Xinput is:
  463. X.nf
  464. X
  465. X    %%
  466. X
  467. X.fi
  468. Xwhich generates a scanner that simply copies its input (one character
  469. Xat a time) to its output.
  470. X.SH ACTIONS
  471. XEach pattern in a rule has a corresponding action, which can be any
  472. Xarbitrary C statement.  The pattern ends at the first non-escaped
  473. Xwhitespace character; the remainder of the line is its action.  If the
  474. Xaction is empty, then when the pattern is matched the input token
  475. Xis simply discarded.  For example, here is the specification for a program
  476. Xwhich deletes all occurrences of "zap me" from its input:
  477. X.nf
  478. X
  479. X    %%
  480. X    "zap me"
  481. X
  482. X.fi
  483. X(It will copy all other characters in the input to the output since
  484. Xthey will be matched by the default rule.)
  485. X.LP
  486. XHere is a program which compresses multiple blanks and tabs down to
  487. Xa single blank, and throws away whitespace found at the end of a line:
  488. X.nf
  489. X
  490. X    %%
  491. X    [ \\t]+        putchar( ' ' );
  492. X    [ \\t]+$       /* ignore this token */
  493. X
  494. X.fi
  495. X.LP
  496. XIf the action contains a '{', then the action spans till the balancing '}'
  497. Xis found, and the action may cross multiple lines.
  498. X.I flex 
  499. Xknows about C strings and comments and won't be fooled by braces found
  500. Xwithin them, but also allows actions to begin with
  501. X.B %{
  502. Xand will consider the action to be all the text up to the next
  503. X.B %}
  504. X(regardless of ordinary braces inside the action).
  505. X.LP
  506. XAn action consisting solely of a vertical bar ('|') means "same as
  507. Xthe action for the next rule."  See below for an illustration.
  508. X.LP
  509. XActions can include arbitrary C code, including
  510. X.B return
  511. Xstatements to return a value to whatever routine called
  512. X.B yylex().
  513. XEach time
  514. X.B yylex()
  515. Xis called it continues processing tokens from where it last left
  516. Xoff until it either reaches
  517. Xthe end of the file or executes a return.  Once it reaches an end-of-file,
  518. Xhowever, then any subsequent call to
  519. X.B yylex()
  520. Xwill simply immediately return, unless
  521. X.B yyrestart()
  522. Xis first called (see below).
  523. X.LP
  524. XActions are not allowed to modify yytext or yyleng.
  525. X.LP
  526. XThere are a number of special directives which can be included within
  527. Xan action:
  528. X.IP -
  529. X.B ECHO
  530. Xcopies yytext to the scanner's output.
  531. X.IP -
  532. X.B BEGIN
  533. Xfollowed by the name of a start condition places the scanner in the
  534. Xcorresponding start condition (see below).
  535. X.IP -
  536. X.B REJECT
  537. Xdirects the scanner to proceed on to the "second best" rule which matched the
  538. Xinput (or a prefix of the input).  The rule is chosen as described
  539. Xabove in "How the Input is Matched", and
  540. X.B yytext
  541. Xand
  542. X.B yyleng
  543. Xset up appropriately.
  544. XIt may either be one which matched as much text
  545. Xas the originally chosen rule but came later in the
  546. X.I flex
  547. Xinput file, or one which matched less text.
  548. XFor example, the following will both count the
  549. Xwords in the input and call the routine special() whenever "frob" is seen:
  550. X.nf
  551. X
  552. X            int word_count = 0;
  553. X    %%
  554. X
  555. X    frob        special(); REJECT;
  556. X    [^ \\t\\n]+   ++word_count;
  557. X
  558. X.fi
  559. XWithout the
  560. X.B REJECT,
  561. Xany "frob"'s in the input would not be counted as words, since the
  562. Xscanner normally executes only one action per token.
  563. XMultiple
  564. X.B REJECT's
  565. Xare allowed, each one finding the next best choice to the currently
  566. Xactive rule.  For example, when the following scanner scans the token
  567. X"abcd", it will write "abcdabcaba" to the output:
  568. X.nf
  569. X
  570. X    %%
  571. X    a        |
  572. X    ab       |
  573. X    abc      |
  574. X    abcd     ECHO; REJECT;
  575. X    .|\\n     /* eat up any unmatched character */
  576. X
  577. X.fi
  578. X(The first three rules share the fourth's action since they use
  579. Xthe special '|' action.)
  580. X.B REJECT
  581. Xis a particularly expensive feature in terms scanner performance;
  582. Xif it is used in
  583. X.I any
  584. Xof the scanner's actions it will slow down
  585. X.I all
  586. Xof the scanner's matching.  Furthermore,
  587. X.B REJECT
  588. Xcannot be used with the
  589. X.I -f
  590. Xor
  591. X.I -F
  592. Xoptions (see below).
  593. X.IP
  594. XNote also that unlike the other special actions,
  595. X.B REJECT
  596. Xis a
  597. X.I branch;
  598. Xcode immediately following it in the action will
  599. X.I not
  600. Xbe executed.
  601. X.IP -
  602. X.B yymore()
  603. Xtells the scanner that the next time it matches a rule, the corresponding
  604. Xtoken should be
  605. X.I appended
  606. Xonto the current value of
  607. X.B yytext
  608. Xrather than replacing it.  For example, given the input "mega-kludge"
  609. Xthe following will write "mega-mega-kludge" to the output:
  610. X.nf
  611. X
  612. X    %%
  613. X    mega-    ECHO; yymore();
  614. X    kludge   ECHO;
  615. X
  616. X.fi
  617. XFirst "mega-" is matched and echoed to the output.  Then "kludge"
  618. Xis matched, but the previous "mega-" is still hanging around at the
  619. Xbeginning of
  620. X.B yytext
  621. Xso the
  622. X.B ECHO
  623. Xfor the "kludge" rule will actually write "mega-kludge".
  624. XThe presence of
  625. X.B yymore()
  626. Xin the scanner's action entails a minor performance penalty in the
  627. Xscanner's matching speed.
  628. X.IP -
  629. X.B yyless(n)
  630. Xreturns all but the first
  631. X.I n
  632. Xcharacters of the current token back to the input stream, where they
  633. Xwill be rescanned when the scanner looks for the next match.
  634. X.B yytext
  635. Xand
  636. X.B yyleng
  637. Xare adjusted appropriately (e.g.,
  638. X.B yyleng
  639. Xwill now be equal to
  640. X.I n
  641. X).  For example, on the input "foobar" the following will write out
  642. X"foobarbar":
  643. X.nf
  644. X
  645. X    %%
  646. X    foobar    ECHO; yyless(3);
  647. X    [a-z]+    ECHO;
  648. X
  649. X.fi
  650. XAn argument of 0 to
  651. X.B yyless
  652. Xwill cause the entire current input string to be scanned again.  Unless you've
  653. Xchanged how the scanner will subsequently process its input (using
  654. X.B BEGIN,
  655. Xfor example), this will result in an endless loop.
  656. X.IP -
  657. X.B unput(c)
  658. Xputs the character
  659. X.I c
  660. Xback onto the input stream.  It will be the next character scanned.
  661. XThe following action will take the current token and cause it
  662. Xto be rescanned enclosed in parentheses.
  663. X.nf
  664. X
  665. X    {
  666. X    int i;
  667. X    unput( ')' );
  668. X    for ( i = yyleng - 1; i >= 0; --i )
  669. X        unput( yytext[i] );
  670. X    unput( '(' );
  671. X    }
  672. X
  673. X.fi
  674. XNote that since each
  675. X.B unput()
  676. Xputs the given character back at the
  677. X.I beginning
  678. Xof the input stream, pushing back strings must be done back-to-front.
  679. X.IP -
  680. X.B input()
  681. Xreads the next character from the input stream.  For example,
  682. Xthe following is one way to eat up C comments:
  683. X.nf
  684. X
  685. X    %%
  686. X    "/*"        {
  687. X                register int c;
  688. X
  689. X                for ( ; ; )
  690. X                    {
  691. X                    while ( (c = input()) != '*' &&
  692. X                            c != EOF )
  693. X                        ;    /* eat up text of comment */
  694. X
  695. X                    if ( c == '*' )
  696. X                        {
  697. X                        while ( (c = input()) == '*' )
  698. X                            ;
  699. X                        if ( c == '/' )
  700. X                            break;    /* found the end */
  701. X                        }
  702. X
  703. X                    if ( c == EOF )
  704. X                        {
  705. X                        error( "EOF in comment" );
  706. X                        break;
  707. X                        }
  708. X                    }
  709. X                }
  710. X
  711. X.fi
  712. X(Note that if the scanner is compiled using
  713. X.B C++,
  714. Xthen
  715. X.B input()
  716. Xis instead referred to as
  717. X.B yyinput(),
  718. Xin order to avoid a name clash with the
  719. X.B C++
  720. Xstream by the name of
  721. X.I input.)
  722. X.IP -
  723. X.B yyterminate()
  724. Xcan be used in lieu of a return statement in an action.  It terminates
  725. Xthe scanner and returns a 0 to the scanner's caller, indicating "all done".
  726. XSubsequent calls to the scanner will immediately return unless preceded
  727. Xby a call to
  728. X.B yyrestart()
  729. X(see below).
  730. XBy default,
  731. X.B yyterminate()
  732. Xis also called when an end-of-file is encountered.  It is a macro and
  733. Xmay be redefined.
  734. X.SH THE GENERATED SCANNER
  735. XThe output of
  736. X.I flex
  737. Xis the file
  738. X.B lex.yy.c,
  739. Xwhich contains the scanning routine
  740. X.B yylex(),
  741. Xa number of tables used by it for matching tokens, and a number
  742. Xof auxiliary routines and macros.  By default,
  743. X.B yylex()
  744. Xis declared as follows:
  745. X.nf
  746. X
  747. X    int yylex()
  748. X        {
  749. X        ... various definitions and the actions in here ...
  750. X        }
  751. X
  752. X.fi
  753. X(If your environment supports function prototypes, then it will
  754. Xbe "int yylex( void )".)  This definition may be changed by redefining
  755. Xthe "YY_DECL" macro.  For example, you could use:
  756. X.nf
  757. X
  758. X    #undef YY_DECL
  759. X    #define YY_DECL float lexscan( a, b ) float a, b;
  760. X
  761. X.fi
  762. Xto give the scanning routine the name
  763. X.I lexscan,
  764. Xreturning a float, and taking two floats as arguments.  Note that
  765. Xif you give arguments to the scanning routine using a
  766. XK&R-style/non-prototyped function declaration, you must terminate
  767. Xthe definition with a semi-colon (;).
  768. X.LP
  769. XWhenever
  770. X.B yylex()
  771. Xis called, it scans tokens from the global input file
  772. X.I yyin
  773. X(which defaults to stdin).  It continues until it either reaches
  774. Xan end-of-file (at which point it returns the value 0) or
  775. Xone of its actions executes a
  776. X.I return
  777. Xstatement.
  778. XIn the former case, when called again the scanner will immediately
  779. Xreturn unless
  780. X.B yyrestart()
  781. Xis called to point
  782. X.I yyin
  783. Xat the new input file.  (
  784. X.B yyrestart()
  785. Xtakes one argument, a
  786. X.B FILE *
  787. Xpointer.)
  788. XIn the latter case (i.e., when an action
  789. Xexecutes a return), the scanner may then be called again and it
  790. Xwill resume scanning where it left off.
  791. X.LP
  792. XBy default (and for purposes of efficiency), the scanner uses
  793. Xblock-reads rather than simple
  794. X.I getc()
  795. Xcalls to read characters from
  796. X.I yyin.
  797. XThe nature of how it gets its input can be controlled by redefining the
  798. X.B YY_INPUT
  799. Xmacro.
  800. XYY_INPUT's calling sequence is "YY_INPUT(buf,result,max_size)".  Its
  801. Xaction is to place up to
  802. X.I max_size
  803. Xcharacters in the character array
  804. X.I buf
  805. Xand return in the integer variable
  806. X.I result
  807. Xeither the
  808. Xnumber of characters read or the constant YY_NULL (0 on Unix systems)
  809. Xto indicate EOF.  The default YY_INPUT reads from the
  810. Xglobal file-pointer "yyin".
  811. X.LP
  812. XA sample redefinition of YY_INPUT (in the definitions
  813. Xsection of the input file):
  814. X.nf
  815. X
  816. X    %{
  817. X    #undef YY_INPUT
  818. X    #define YY_INPUT(buf,result,max_size) \\
  819. X        { \\
  820. X        int c = getchar(); \\
  821. X        result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \\
  822. X        }
  823. X    %}
  824. X
  825. X.fi
  826. XThis definition will change the input processing to occur
  827. Xone character at a time.
  828. X.LP
  829. XYou also can add in things like keeping track of the
  830. Xinput line number this way; but don't expect your scanner to
  831. Xgo very fast.
  832. X.LP
  833. XWhen the scanner receives an end-of-file indication from YY_INPUT,
  834. Xit then checks the
  835. X.B yywrap()
  836. Xfunction.  If
  837. X.B yywrap()
  838. Xreturns false (zero), then it is assumed that the
  839. Xfunction has gone ahead and set up
  840. X.I yyin
  841. Xto point to another input file, and scanning continues.  If it returns
  842. Xtrue (non-zero), then the scanner terminates, returning 0 to its
  843. Xcaller.
  844. X.LP
  845. XThe default
  846. X.B yywrap()
  847. Xalways returns 1.  Presently, to redefine it you must first
  848. X"#undef yywrap", as it is currently implemented as a macro.  As indicated
  849. Xby the hedging in the previous sentence, it may be changed to
  850. Xa true function in the near future.
  851. X.LP
  852. XThe scanner writes its
  853. X.B ECHO
  854. Xoutput to the
  855. X.I yyout
  856. Xglobal (default, stdout), which may be redefined by the user simply
  857. Xby assigning it to some other
  858. X.B FILE
  859. Xpointer.
  860. X.SH START CONDITIONS
  861. X.I flex
  862. Xprovides a mechanism for conditionally activating rules.  Any rule
  863. Xwhose pattern is prefixed with "<sc>" will only be active when
  864. Xthe scanner is in the start condition named "sc".  For example,
  865. X.nf
  866. X
  867. X    <STRING>[^"]*        { /* eat up the string body ... */
  868. X                ...
  869. X                }
  870. X
  871. X.fi
  872. Xwill be active only when the scanner is in the "STRING" start
  873. Xcondition, and
  874. X.nf
  875. X
  876. X    <INITIAL,STRING,QUOTE>\\.        { /* handle an escape ... */
  877. X                ...
  878. X                }
  879. X
  880. X.fi
  881. Xwill be active only when the current start condition is
  882. Xeither "INITIAL", "STRING", or "QUOTE".
  883. X.LP
  884. XStart conditions
  885. Xare declared in the definitions (first) section of the input
  886. Xusing unindented lines beginning with either
  887. X.B %s
  888. Xor
  889. X.B %x
  890. Xfollowed by a list of names.
  891. XThe former declares
  892. X.I inclusive
  893. Xstart conditions, the latter
  894. X.I exclusive
  895. Xstart conditions.  A start condition is activated using the
  896. X.B BEGIN
  897. Xaction.  Until the next
  898. X.B BEGIN
  899. Xaction is executed, rules with the given start
  900. Xcondition will be active and
  901. Xrules with other start conditions will be inactive.
  902. XIf the start condition is
  903. X.I inclusive,
  904. Xthen rules with no start conditions at all will also be active.
  905. XIf it is
  906. X.I exclusive,
  907. Xthen
  908. X.I only
  909. Xrules qualified with the start condition will be active.
  910. XA set of rules contingent on the same exclusive start condition
  911. Xdescribe a scanner which is independent of any of the other rules in the
  912. X.I flex
  913. Xinput.  Because of this,
  914. Xexclusive start conditions make it easy to specify "mini-scanners"
  915. Xwhich scan portions of the input that are syntactically different
  916. Xfrom the rest (e.g., comments).
  917. X.LP
  918. XIf the distinction between inclusive and exclusive start conditions
  919. Xis still a little vague, here's a simple example illustrating the
  920. Xconnection between the two.  The set of rules:
  921. X.nf
  922. X
  923. X    %s example
  924. X    %%
  925. X    <example>foo           /* do something */
  926. X
  927. X.fi
  928. Xis equivalent to
  929. X.nf
  930. X
  931. X    %x example
  932. X    %%
  933. X    <INITIAL,example>foo   /* do something */
  934. X
  935. X.fi
  936. X.LP
  937. XThe default rule (to
  938. X.B ECHO
  939. Xany unmatched character) remains active in start conditions.
  940. X.LP
  941. X.B BEGIN(0)
  942. Xreturns to the original state where only the rules with
  943. Xno start conditions are active.  This state can also be
  944. Xreferred to as the start-condition "INITIAL", so
  945. X.B BEGIN(INITIAL)
  946. Xis equivalent to
  947. X.B BEGIN(0).
  948. X(The parentheses around the start condition name are not required but
  949. Xare considered good style.)
  950. X.LP
  951. X.B BEGIN
  952. Xactions can also be given as indented code at the beginning
  953. Xof the rules section.  For example, the following will cause
  954. Xthe scanner to enter the "SPECIAL" start condition whenever
  955. X.I yylex()
  956. Xis called and the global variable
  957. X.I enter_special
  958. Xis true:
  959. X.nf
  960. X
  961. X            int enter_special;
  962. X
  963. X    %x SPECIAL
  964. X    %%
  965. X            if ( enter_special )
  966. X                BEGIN(SPECIAL);
  967. X
  968. X    <SPECIAL>blahblahblah
  969. X    ...more rules follow...
  970. X
  971. X.fi
  972. X.LP
  973. XTo illustrate the uses of start conditions,
  974. Xhere is a scanner which provides two different interpretations
  975. Xof a string like "123.456".  By default it will treat it as
  976. Xas three tokens, the integer "123", a dot ('.'), and the integer "456".
  977. XBut if the string is preceded earlier in the line by the string
  978. X"expect-floats"
  979. Xit will treat it as a single token, the floating-point number
  980. X123.456:
  981. X.nf
  982. X
  983. X    %{
  984. X    #include <math.h>
  985. X    %}
  986. X    %s expect
  987. X
  988. X    %%
  989. X    expect-floats        BEGIN(expect);
  990. X
  991. X    <expect>[0-9]+"."[0-9]+      {
  992. X                printf( "found a float, = %f\\n",
  993. X                        atof( yytext ) );
  994. X                }
  995. X    <expect>\\n           {
  996. X                /* that's the end of the line, so
  997. X                 * we need another "expect-number"
  998. X                 * before we'll recognize any more
  999. X                 * numbers
  1000. X                 */
  1001. X                BEGIN(INITIAL);
  1002. X                }
  1003. X
  1004. X    [0-9]+      {
  1005. X                printf( "found an integer, = %d\\n",
  1006. X                        atoi( yytext ) );
  1007. X                }
  1008. X
  1009. X    "."         printf( "found a dot\\n" );
  1010. X
  1011. X.fi
  1012. XHere is a scanner which recognizes (and discards) C comments while
  1013. Xmaintaining a count of the current input line.
  1014. X.nf
  1015. X
  1016. X    %x comment
  1017. X    %%
  1018. X            int line_num = 1;
  1019. X
  1020. X    "/*"         BEGIN(comment);
  1021. X
  1022. X    <comment>[^*\\n]*        /* eat anything that's not a '*' */
  1023. X    <comment>"*"+[^*/\\n]*   /* eat up '*'s not followed by '/'s */
  1024. X    <comment>\\n             ++line_num;
  1025. X    <comment>"*"+"/"        BEGIN(INITIAL);
  1026. X
  1027. X.fi
  1028. XNote that start-conditions names are really integer values and
  1029. Xcan be stored as such.  Thus, the above could be extended in the
  1030. Xfollowing fashion:
  1031. X.nf
  1032. X
  1033. X    %x comment foo
  1034. X    %%
  1035. X            int line_num = 1;
  1036. X            int comment_caller;
  1037. X
  1038. X    "/*"         {
  1039. X                 comment_caller = INITIAL;
  1040. X                 BEGIN(comment);
  1041. X                 }
  1042. X
  1043. X    ...
  1044. X
  1045. X    <foo>"/*"    {
  1046. X                 comment_caller = foo;
  1047. X                 BEGIN(comment);
  1048. X                 }
  1049. X
  1050. X    <comment>[^*\\n]*        /* eat anything that's not a '*' */
  1051. X    <comment>"*"+[^*/\\n]*   /* eat up '*'s not followed by '/'s */
  1052. X    <comment>\\n             ++line_num;
  1053. X    <comment>"*"+"/"        BEGIN(comment_caller);
  1054. X
  1055. X.fi
  1056. XOne can then implement a "stack" of start conditions using an
  1057. Xarray of integers.  (It is likely that such stacks will become
  1058. Xa full-fledged
  1059. X.I flex
  1060. Xfeature in the future.)  Note, though, that
  1061. Xstart conditions do not have their own name-space; %s's and %x's
  1062. Xdeclare names in the same fashion as #define's.
  1063. X.SH MULTIPLE INPUT BUFFERS
  1064. XSome scanners (such as those which support "include" files)
  1065. Xrequire reading from several input streams.  As
  1066. X.I flex
  1067. Xscanners do a large amount of buffering, one cannot control
  1068. Xwhere the next input will be read from by simply writing a
  1069. X.B YY_INPUT
  1070. Xwhich is sensitive to the scanning context.
  1071. X.B YY_INPUT
  1072. Xis only called when the scanner reaches the end of its buffer, which
  1073. Xmay be a long time after scanning a statement such as an "include"
  1074. Xwhich requires switching the input source.
  1075. X.LP
  1076. XTo negotiate these sorts of problems,
  1077. X.I flex
  1078. Xprovides a mechanism for creating and switching between multiple
  1079. Xinput buffers.  An input buffer is created by using:
  1080. X.nf
  1081. X
  1082. X    YY_BUFFER_STATE yy_create_buffer( FILE *file, int size )
  1083. X
  1084. X.fi
  1085. Xwhich takes a
  1086. X.I FILE
  1087. Xpointer and a size and creates a buffer associated with the given
  1088. Xfile and large enough to hold
  1089. X.I size
  1090. Xcharacters (when in doubt, use
  1091. X.B YY_BUF_SIZE
  1092. Xfor the size).  It returns a
  1093. X.B YY_BUFFER_STATE
  1094. Xhandle, which may then be passed to other routines:
  1095. X.nf
  1096. X
  1097. X    void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer )
  1098. X
  1099. X.fi
  1100. Xswitches the scanner's input buffer so subsequent tokens will
  1101. Xcome from
  1102. X.I new_buffer.
  1103. XNote that
  1104. X.B yy_switch_to_buffer()
  1105. Xmay be used by yywrap() to sets things up for continued scanning, instead
  1106. Xof opening a new file and pointing
  1107. X.I yyin
  1108. Xat it.
  1109. X.nf
  1110. X
  1111. X    void yy_delete_buffer( YY_BUFFER_STATE buffer )
  1112. X
  1113. X.fi
  1114. Xis used to reclaim the storage associated with a buffer.
  1115. X.LP
  1116. X.B yy_new_buffer()
  1117. Xis an alias for
  1118. X.B yy_create_buffer(),
  1119. Xprovided for compatibility with the C++ use of
  1120. X.I new
  1121. Xand
  1122. X.I delete
  1123. Xfor creating and destroying dynamic objects.
  1124. X.LP
  1125. XFinally, the
  1126. X.B YY_CURRENT_BUFFER
  1127. Xmacro returns a
  1128. X.B YY_BUFFER_STATE
  1129. Xhandle to the current buffer.
  1130. X.LP
  1131. XHere is an example of using these features for writing a scanner
  1132. Xwhich expands include files (the
  1133. X.B <<EOF>>
  1134. Xfeature is discussed below):
  1135. X.nf
  1136. X
  1137. X    /* the "incl" state is used for picking up the name
  1138. X     * of an include file
  1139. X     */
  1140. X    %x incl
  1141. X
  1142. X    %{
  1143. X    #define MAX_INCLUDE_DEPTH 10
  1144. X    YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
  1145. X    int include_stack_ptr = 0;
  1146. X    %}
  1147. X
  1148. X    %%
  1149. X    include             BEGIN(incl);
  1150. X
  1151. X    [a-z]+              ECHO;
  1152. X    [^a-z\\n]*\\n?        ECHO;
  1153. X
  1154. X    <incl>[ \\t]*      /* eat the whitespace */
  1155. X    <incl>[^ \\t\\n]+   { /* got the include file name */
  1156. X            if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
  1157. X                {
  1158. X                fprintf( stderr, "Includes nested too deeply" );
  1159. X                exit( 1 );
  1160. X                }
  1161. X
  1162. X            include_stack[include_stack_ptr++] =
  1163. X                YY_CURRENT_BUFFER;
  1164. X
  1165. X            yyin = fopen( yytext, "r" );
  1166. X
  1167. X            if ( ! yyin )
  1168. X                error( ... );
  1169. X
  1170. X            yy_switch_to_buffer(
  1171. X                yy_create_buffer( yyin, YY_BUF_SIZE ) );
  1172. X
  1173. X            BEGIN(INITIAL);
  1174. X            }
  1175. X
  1176. X    <<EOF>> {
  1177. X            if ( --include_stack_ptr < 0 )
  1178. X                {
  1179. X                yyterminate();
  1180. X                }
  1181. X
  1182. X            else
  1183. X                yy_switch_to_buffer(
  1184. X                     include_stack[include_stack_ptr] );
  1185. X            }
  1186. X
  1187. X.fi
  1188. X.SH END-OF-FILE RULES
  1189. XThe special rule "<<EOF>>" indicates
  1190. Xactions which are to be taken when an end-of-file is
  1191. Xencountered and yywrap() returns non-zero (i.e., indicates
  1192. Xno further files to process).  The action must finish
  1193. Xby doing one of four things:
  1194. X.IP -
  1195. Xthe special
  1196. X.B YY_NEW_FILE
  1197. Xaction, if
  1198. X.I yyin
  1199. Xhas been pointed at a new file to process;
  1200. X.IP -
  1201. Xa
  1202. X.I return
  1203. Xstatement;
  1204. X.IP -
  1205. Xthe special
  1206. X.B yyterminate()
  1207. Xaction;
  1208. X.IP -
  1209. Xor, switching to a new buffer using
  1210. X.B yy_switch_to_buffer()
  1211. Xas shown in the example above.
  1212. X.LP
  1213. X<<EOF>> rules may not be used with other
  1214. Xpatterns; they may only be qualified with a list of start
  1215. Xconditions.  If an unqualified <<EOF>> rule is given, it
  1216. Xapplies to
  1217. X.I all
  1218. Xstart conditions which do not already have <<EOF>> actions.  To
  1219. Xspecify an <<EOF>> rule for only the initial start condition, use
  1220. X.nf
  1221. X
  1222. X    <INITIAL><<EOF>>
  1223. X
  1224. X.fi
  1225. X.LP
  1226. XThese rules are useful for catching things like unclosed comments.
  1227. XAn example:
  1228. X.nf
  1229. X
  1230. X    %x quote
  1231. X    %%
  1232. X
  1233. X    ...other rules for dealing with quotes...
  1234. X
  1235. X    <quote><<EOF>>   {
  1236. X             error( "unterminated quote" );
  1237. X             yyterminate();
  1238. X             }
  1239. X    <<EOF>>  {
  1240. X             if ( *++filelist )
  1241. X                 {
  1242. X                 yyin = fopen( *filelist, "r" );
  1243. X                 YY_NEW_FILE;
  1244. X                 }
  1245. X             else
  1246. X                yyterminate();
  1247. X             }
  1248. X
  1249. X.fi
  1250. X.SH MISCELLANEOUS MACROS
  1251. XThe macro
  1252. X.bd
  1253. XYY_USER_ACTION
  1254. Xcan be redefined to provide an action
  1255. Xwhich is always executed prior to the matched rule's action.  For example,
  1256. Xit could be #define'd to call a routine to convert yytext to lower-case.
  1257. X.LP
  1258. XThe macro
  1259. X.B YY_USER_INIT
  1260. Xmay be redefined to provide an action which is always executed before
  1261. Xthe first scan (and before the scanner's internal initializations are done).
  1262. XFor example, it could be used to call a routine to read
  1263. Xin a data table or open a logging file.
  1264. X.LP
  1265. XIn the generated scanner, the actions are all gathered in one large
  1266. Xswitch statement and separated using
  1267. X.B YY_BREAK,
  1268. Xwhich may be redefined.  By default, it is simply a "break", to separate
  1269. Xeach rule's action from the following rule's.
  1270. XRedefining
  1271. X.B YY_BREAK
  1272. Xallows, for example, C++ users to
  1273. X#define YY_BREAK to do nothing (while being very careful that every
  1274. Xrule ends with a "break" or a "return"!) to avoid suffering from
  1275. Xunreachable statement warnings where because a rule's action ends with
  1276. X"return", the
  1277. X.B YY_BREAK
  1278. Xis inaccessible.
  1279. X.SH INTERFACING WITH YACC
  1280. XOne of the main uses of
  1281. X.I flex
  1282. Xis as a companion to the
  1283. X.I yacc
  1284. Xparser-generator.
  1285. X.I yacc
  1286. Xparsers expect to call a routine named
  1287. X.B yylex()
  1288. Xto find the next input token.  The routine is supposed to
  1289. Xreturn the type of the next token as well as putting any associated
  1290. Xvalue in the global
  1291. X.B yylval.
  1292. XTo use
  1293. X.I flex
  1294. Xwith
  1295. X.I yacc,
  1296. Xone specifies the
  1297. X.B -d
  1298. Xoption to
  1299. X.I yacc
  1300. Xto instruct it to generate the file
  1301. X.B y.tab.h
  1302. Xcontaining definitions of all the
  1303. X.B %tokens
  1304. Xappearing in the
  1305. X.I yacc
  1306. Xinput.  This file is then included in the
  1307. X.I flex
  1308. Xscanner.  For example, if one of the tokens is "TOK_NUMBER",
  1309. Xpart of the scanner might look like:
  1310. X.nf
  1311. X
  1312. X    %{
  1313. X    #include "y.tab.h"
  1314. X    %}
  1315. X
  1316. X    %%
  1317. X
  1318. X    [0-9]+        yylval = atoi( yytext ); return TOK_NUMBER;
  1319. X
  1320. X.fi
  1321. X.SH TRANSLATION TABLE
  1322. XIn the name of POSIX compliance,
  1323. X.I flex
  1324. Xsupports a
  1325. X.I translation table
  1326. Xfor mapping input characters into groups.
  1327. XThe table is specified in the first section, and its format looks like:
  1328. X.nf
  1329. X
  1330. X    %t
  1331. X    1        abcd
  1332. X    2        ABCDEFGHIJKLMNOPQRSTUVWXYZ
  1333. X    52       0123456789
  1334. X    6        \\t\\ \\n
  1335. X    %t
  1336. X
  1337. X.fi
  1338. XThis example specifies that the characters 'a', 'b', 'c', and 'd'
  1339. Xare to all be lumped into group #1, upper-case letters
  1340. Xin group #2, digits in group #52, tabs, blanks, and newlines into
  1341. Xgroup #6, and
  1342. X.I
  1343. Xno other characters will appear in the patterns.
  1344. XThe group numbers are actually disregarded by
  1345. X.I flex;
  1346. X.B %t
  1347. Xserves, though, to lump characters together.  Given the above
  1348. Xtable, for example, the pattern "a(AA)*5" is equivalent to "d(ZQ)*0".
  1349. XThey both say, "match any character in group #1, followed by
  1350. Xzero-or-more pairs of characters
  1351. Xfrom group #2, followed by a character from group #52."  Thus
  1352. X.B %t
  1353. Xprovides a crude way for introducing equivalence classes into
  1354. Xthe scanner specification.
  1355. X.LP
  1356. XNote that the
  1357. X.B -i
  1358. Xoption (see below) coupled with the equivalence classes which
  1359. X.I flex
  1360. Xautomatically generates take care of virtually all the instances
  1361. Xwhen one might consider using
  1362. X.B %t.
  1363. XBut what the hell, it's there if you want it.
  1364. X.SH OPTIONS
  1365. X.I flex
  1366. Xhas the following options:
  1367. X.TP
  1368. X.B -b
  1369. XGenerate backtracking information to
  1370. X.I lex.backtrack.
  1371. XThis is a list of scanner states which require backtracking
  1372. Xand the input characters on which they do so.  By adding rules one
  1373. Xcan remove backtracking states.  If all backtracking states
  1374. Xare eliminated and
  1375. X.B -f
  1376. Xor
  1377. X.B -F
  1378. Xis used, the generated scanner will run faster (see the
  1379. X.B -p
  1380. Xflag).  Only users who wish to squeeze every last cycle out of their
  1381. Xscanners need worry about this option.  (See the section on PERFORMANCE
  1382. XCONSIDERATIONS below.)
  1383. X.TP
  1384. X.B -c
  1385. Xis a do-nothing, deprecated option included for POSIX compliance.
  1386. X.IP
  1387. X.B NOTE:
  1388. Xin previous releases of
  1389. X.I flex
  1390. X.B -c
  1391. Xspecified table-compression options.  This functionality is
  1392. Xnow given by the
  1393. X.B -C
  1394. Xflag.  To ease the the impact of this change, when
  1395. X.I flex
  1396. Xencounters
  1397. X.B -c,
  1398. Xit currently issues a warning message and assumes that
  1399. X.B -C
  1400. Xwas desired instead.  In the future this "promotion" of
  1401. X.B -c
  1402. Xto
  1403. X.B -C
  1404. Xwill go away in the name of full POSIX compliance (unless
  1405. Xthe POSIX meaning is removed first).
  1406. X.TP
  1407. X.B -d
  1408. Xmakes the generated scanner run in
  1409. X.I debug
  1410. Xmode.  Whenever a pattern is recognized and the global
  1411. X.B yy_flex_debug
  1412. Xis non-zero (which is the default),
  1413. Xthe scanner will write to
  1414. X.I stderr
  1415. Xa line of the form:
  1416. X.nf
  1417. X
  1418. X    --accepting rule at line 53 ("the matched text")
  1419. X
  1420. X.fi
  1421. XThe line number refers to the location of the rule in the file
  1422. Xdefining the scanner (i.e., the file that was fed to flex).  Messages
  1423. Xare also generated when the scanner backtracks, accepts the
  1424. Xdefault rule, reaches the end of its input buffer (or encounters
  1425. Xa NUL; at this point, the two look the same as far as the scanner's concerned),
  1426. Xor reaches an end-of-file.
  1427. X.TP
  1428. X.B -f
  1429. Xspecifies (take your pick)
  1430. X.I full table
  1431. Xor
  1432. X.I fast scanner.
  1433. XNo table compression is done.  The result is large but fast.
  1434. XThis option is equivalent to
  1435. X.B -Cf
  1436. X(see below).
  1437. X.TP
  1438. X.B -i
  1439. Xinstructs
  1440. X.I flex
  1441. Xto generate a
  1442. X.I case-insensitive
  1443. Xscanner.  The case of letters given in the
  1444. X.I flex
  1445. Xinput patterns will
  1446. Xbe ignored, and tokens in the input will be matched regardless of case.  The
  1447. Xmatched text given in
  1448. X.I yytext
  1449. Xwill have the preserved case (i.e., it will not be folded).
  1450. X.TP
  1451. X.B -n
  1452. Xis another do-nothing, deprecated option included only for
  1453. XPOSIX compliance.
  1454. X.TP
  1455. X.B -p
  1456. Xgenerates a performance report to stderr.  The report
  1457. Xconsists of comments regarding features of the
  1458. X.I flex
  1459. Xinput file which will cause a loss of performance in the resulting scanner.
  1460. XNote that the use of
  1461. X.I REJECT
  1462. Xand variable trailing context (see the BUGS section in flex(1))
  1463. Xentails a substantial performance penalty; use of
  1464. X.I yymore(),
  1465. Xthe
  1466. X.B ^
  1467. Xoperator,
  1468. Xand the
  1469. X.B -I
  1470. Xflag entail minor performance penalties.
  1471. X.TP
  1472. X.B -s
  1473. Xcauses the
  1474. X.I default rule
  1475. X(that unmatched scanner input is echoed to
  1476. X.I stdout)
  1477. Xto be suppressed.  If the scanner encounters input that does not
  1478. Xmatch any of its rules, it aborts with an error.  This option is
  1479. Xuseful for finding holes in a scanner's rule set.
  1480. X.TP
  1481. X.B -t
  1482. Xinstructs
  1483. X.I flex
  1484. Xto write the scanner it generates to standard output instead
  1485. Xof
  1486. X.B lex.yy.c.
  1487. X.TP
  1488. X.B -v
  1489. Xspecifies that
  1490. X.I flex
  1491. Xshould write to
  1492. X.I stderr
  1493. Xa summary of statistics regarding the scanner it generates.
  1494. XMost of the statistics are meaningless to the casual
  1495. X.I flex
  1496. Xuser, but the
  1497. Xfirst line identifies the version of
  1498. X.I flex,
  1499. Xwhich is useful for figuring
  1500. Xout where you stand with respect to patches and new releases,
  1501. Xand the next two lines give the date when the scanner was created
  1502. Xand a summary of the flags which were in effect.
  1503. X.TP
  1504. X.B -F
  1505. Xspecifies that the
  1506. X.ul
  1507. Xfast
  1508. Xscanner table representation should be used.  This representation is
  1509. Xabout as fast as the full table representation
  1510. X.ul
  1511. X(-f),
  1512. Xand for some sets of patterns will be considerably smaller (and for
  1513. Xothers, larger).  In general, if the pattern set contains both "keywords"
  1514. Xand a catch-all, "identifier" rule, such as in the set:
  1515. X.nf
  1516. X
  1517. X    "case"    return TOK_CASE;
  1518. X    "switch"  return TOK_SWITCH;
  1519. X    ...
  1520. X    "default" return TOK_DEFAULT;
  1521. X    [a-z]+    return TOK_ID;
  1522. X
  1523. X.fi
  1524. Xthen you're better off using the full table representation.  If only
  1525. Xthe "identifier" rule is present and you then use a hash table or some such
  1526. Xto detect the keywords, you're better off using
  1527. X.ul
  1528. X-F.
  1529. X.IP
  1530. XThis option is equivalent to
  1531. X.B -CF
  1532. X(see below).
  1533. X.TP
  1534. X.B -I
  1535. Xinstructs
  1536. X.I flex
  1537. Xto generate an
  1538. X.I interactive
  1539. Xscanner.  Normally, scanners generated by
  1540. X.I flex
  1541. Xalways look ahead one
  1542. Xcharacter before deciding that a rule has been matched.  At the cost of
  1543. Xsome scanning overhead,
  1544. X.I flex
  1545. Xwill generate a scanner which only looks ahead
  1546. Xwhen needed.  Such scanners are called
  1547. X.I interactive
  1548. Xbecause if you want to write a scanner for an interactive system such as a
  1549. Xcommand shell, you will probably want the user's input to be terminated
  1550. Xwith a newline, and without
  1551. X.B -I
  1552. Xthe user will have to type a character in addition to the newline in order
  1553. Xto have the newline recognized.  This leads to dreadful interactive
  1554. Xperformance.
  1555. X.IP
  1556. XIf all this seems to confusing, here's the general rule: if a human will
  1557. Xbe typing in input to your scanner, use
  1558. X.B -I,
  1559. Xotherwise don't; if you don't care about squeezing the utmost performance
  1560. Xfrom your scanner and you
  1561. Xdon't want to make any assumptions about the input to your scanner,
  1562. Xuse
  1563. X.B -I.
  1564. X.IP
  1565. XNote,
  1566. X.B -I
  1567. Xcannot be used in conjunction with
  1568. X.I full
  1569. Xor
  1570. X.I fast tables,
  1571. Xi.e., the
  1572. X.B -f, -F, -Cf,
  1573. Xor
  1574. X.B -CF
  1575. Xflags.
  1576. X.TP
  1577. X.B -L
  1578. Xinstructs
  1579. X.I flex
  1580. Xnot to generate
  1581. X.B #line
  1582. Xdirectives.  Without this option,
  1583. X.I flex
  1584. Xpeppers the generated scanner
  1585. Xwith #line directives so error messages in the actions will be correctly
  1586. Xlocated with respect to the original
  1587. X.I flex
  1588. Xinput file, and not to
  1589. Xthe fairly meaningless line numbers of
  1590. X.B lex.yy.c.
  1591. X(Unfortunately
  1592. X.I flex
  1593. Xdoes not presently generate the necessary directives
  1594. Xto "retarget" the line numbers for those parts of
  1595. X.B lex.yy.c
  1596. Xwhich it generated.  So if there is an error in the generated code,
  1597. Xa meaningless line number is reported.)
  1598. X.TP
  1599. X.B -T
  1600. Xmakes
  1601. X.I flex
  1602. Xrun in
  1603. X.I trace
  1604. Xmode.  It will generate a lot of messages to
  1605. X.I stdout
  1606. Xconcerning
  1607. Xthe form of the input and the resultant non-deterministic and deterministic
  1608. Xfinite automata.  This option is mostly for use in maintaining
  1609. X.I flex.
  1610. X.TP
  1611. X.B -8
  1612. Xinstructs
  1613. X.I flex
  1614. Xto generate an 8-bit scanner, i.e., one which can recognize 8-bit
  1615. Xcharacters.  On some sites,
  1616. X.I flex
  1617. Xis installed with this option as the default.  On others, the default
  1618. Xis 7-bit characters.  To see which is the case, check the verbose
  1619. X.B (-v)
  1620. Xoutput for "equivalence classes created".  If the denominator of
  1621. Xthe number shown is 128, then by default
  1622. X.I flex
  1623. Xis generating 7-bit characters.  If it is 256, then the default is
  1624. X8-bit characters and the
  1625. X.B -8
  1626. Xflag is not required (but may be a good idea to keep the scanner
  1627. Xspecification portable).  Feeding a 7-bit scanner 8-bit characters
  1628. Xwill result in infinite loops, bus errors, or other such fireworks,
  1629. Xso when in doubt, use the flag.  Note that if equivalence classes
  1630. Xare used, 8-bit scanners take only slightly more table space than
  1631. X7-bit scanners (128 bytes, to be exact); if equivalence classes are
  1632. Xnot used, however, then the tables may grow up to twice their
  1633. X7-bit size.
  1634. X.TP 
  1635. X.B -C[efmF]
  1636. Xcontrols the degree of table compression.
  1637. X.IP
  1638. X.B -Ce
  1639. Xdirects
  1640. X.I flex
  1641. Xto construct
  1642. X.I equivalence classes,
  1643. Xi.e., sets of characters
  1644. Xwhich have identical lexical properties (for example, if the only
  1645. Xappearance of digits in the
  1646. X.I flex
  1647. Xinput is in the character class
  1648. X"[0-9]" then the digits '0', '1', ..., '9' will all be put
  1649. Xin the same equivalence class).  Equivalence classes usually give
  1650. Xdramatic reductions in the final table/object file sizes (typically
  1651. Xa factor of 2-5) and are pretty cheap performance-wise (one array
  1652. Xlook-up per character scanned).
  1653. X.IP
  1654. X.B -Cf
  1655. Xspecifies that the
  1656. X.I full
  1657. Xscanner tables should be generated -
  1658. X.I flex
  1659. Xshould not compress the
  1660. Xtables by taking advantages of similar transition functions for
  1661. Xdifferent states.
  1662. X.IP
  1663. X.B -CF
  1664. Xspecifies that the alternate fast scanner representation (described
  1665. Xabove under the
  1666. X.B -F
  1667. Xflag)
  1668. Xshould be used.
  1669. X.IP
  1670. X.B -Cm
  1671. Xdirects
  1672. X.I flex
  1673. Xto construct
  1674. X.I meta-equivalence classes,
  1675. Xwhich are sets of equivalence classes (or characters, if equivalence
  1676. Xclasses are not being used) that are commonly used together.  Meta-equivalence
  1677. Xclasses are often a big win when using compressed tables, but they
  1678. Xhave a moderate performance impact (one or two "if" tests and one
  1679. Xarray look-up per character scanned).
  1680. X.IP
  1681. XA lone
  1682. X.B -C
  1683. Xspecifies that the scanner tables should be compressed but neither
  1684. Xequivalence classes nor meta-equivalence classes should be used.
  1685. X.IP
  1686. XThe options
  1687. X.B -Cf
  1688. Xor
  1689. X.B -CF
  1690. Xand
  1691. X.B -Cm
  1692. Xdo not make sense together - there is no opportunity for meta-equivalence
  1693. Xclasses if the table is not being compressed.  Otherwise the options
  1694. Xmay be freely mixed.
  1695. X.IP
  1696. XThe default setting is
  1697. X.B -Cem,
  1698. Xwhich specifies that
  1699. X.I flex
  1700. Xshould generate equivalence classes
  1701. Xand meta-equivalence classes.  This setting provides the highest
  1702. Xdegree of table compression.  You can trade off
  1703. Xfaster-executing scanners at the cost of larger tables with
  1704. Xthe following generally being true:
  1705. X.nf
  1706. X
  1707. X    slowest & smallest
  1708. X          -Cem
  1709. X          -Cm
  1710. X          -Ce
  1711. X          -C
  1712. X          -C{f,F}e
  1713. X          -C{f,F}
  1714. X    fastest & largest
  1715. X
  1716. X.fi
  1717. XNote that scanners with the smallest tables are usually generated and
  1718. Xcompiled the quickest, so
  1719. Xduring development you will usually want to use the default, maximal
  1720. Xcompression.
  1721. X.IP
  1722. X.B -Cfe
  1723. Xis often a good compromise between speed and size for production
  1724. Xscanners.
  1725. X.IP
  1726. X.B -C
  1727. Xoptions are not cumulative; whenever the flag is encountered, the
  1728. Xprevious -C settings are forgotten.
  1729. X.TP
  1730. X.B -Sskeleton_file
  1731. Xoverrides the default skeleton file from which
  1732. X.I flex
  1733. Xconstructs its scanners.  You'll never need this option unless you are doing
  1734. X.I flex
  1735. Xmaintenance or development.
  1736. X.SH PERFORMANCE CONSIDERATIONS
  1737. XThe main design goal of
  1738. X.I flex
  1739. Xis that it generate high-performance scanners.  It has been optimized
  1740. Xfor dealing well with large sets of rules.  Aside from the effects
  1741. Xof table compression on scanner speed outlined above,
  1742. Xthere are a number of options/actions which degrade performance.  These
  1743. Xare, from most expensive to least:
  1744. X.nf
  1745. X
  1746. X    REJECT
  1747. X
  1748. X    pattern sets that require backtracking
  1749. X    arbitrary trailing context
  1750. X
  1751. X    '^' beginning-of-line operator
  1752. X    yymore()
  1753. X
  1754. X.fi
  1755. Xwith the first three all being quite expensive and the last two
  1756. Xbeing quite cheap.
  1757. X.LP
  1758. X.B REJECT
  1759. Xshould be avoided at all costs when performance is important.
  1760. XIt is a particularly expensive option.
  1761. X.LP
  1762. XGetting rid of backtracking is messy and often may be an enormous
  1763. Xamount of work for a complicated scanner.  In principal, one begins
  1764. Xby using the
  1765. X.B -b 
  1766. Xflag to generate a
  1767. X.I lex.backtrack
  1768. Xfile.  For example, on the input
  1769. X.nf
  1770. X
  1771. X    %%
  1772. X    foo        return TOK_KEYWORD;
  1773. X    foobar     return TOK_KEYWORD;
  1774. X
  1775. X.fi
  1776. Xthe file looks like:
  1777. X.nf
  1778. X
  1779. X    State #6 is non-accepting -
  1780. X     associated rule line numbers:
  1781. X           2       3
  1782. X     out-transitions: [ o ]
  1783. X     jam-transitions: EOF [ \\001-n  p-\\177 ]
  1784. X
  1785. X    State #8 is non-accepting -
  1786. X     associated rule line numbers:
  1787. X           3
  1788. X     out-transitions: [ a ]
  1789. X     jam-transitions: EOF [ \\001-`  b-\\177 ]
  1790. X
  1791. X    State #9 is non-accepting -
  1792. X     associated rule line numbers:
  1793. X           3
  1794. X     out-transitions: [ r ]
  1795. X     jam-transitions: EOF [ \\001-q  s-\\177 ]
  1796. X
  1797. X    Compressed tables always backtrack.
  1798. X
  1799. X.fi
  1800. XThe first few lines tell us that there's a scanner state in
  1801. Xwhich it can make a transition on an 'o' but not on any other
  1802. Xcharacter, and that in that state the currently scanned text does not match
  1803. Xany rule.  The state occurs when trying to match the rules found
  1804. Xat lines 2 and 3 in the input file.
  1805. XIf the scanner is in that state and then reads
  1806. Xsomething other than an 'o', it will have to backtrack to find
  1807. Xa rule which is matched.  With
  1808. Xa bit of headscratching one can see that this must be the
  1809. Xstate it's in when it has seen "fo".  When this has happened,
  1810. Xif anything other than another 'o' is seen, the scanner will
  1811. Xhave to back up to simply match the 'f' (by the default rule).
  1812. X.LP
  1813. XThe comment regarding State #8 indicates there's a problem
  1814. Xwhen "foob" has been scanned.  Indeed, on any character other
  1815. Xthan a 'b', the scanner will have to back up to accept "foo".
  1816. XSimilarly, the comment for State #9 concerns when "fooba" has
  1817. Xbeen scanned.
  1818. X.LP
  1819. XThe final comment reminds us that there's no point going to
  1820. Xall the trouble of removing backtracking from the rules unless
  1821. Xwe're using
  1822. X.B -f
  1823. Xor
  1824. X.B -F,
  1825. Xsince there's no performance gain doing so with compressed scanners.
  1826. X.LP
  1827. XThe way to remove the backtracking is to add "error" rules:
  1828. X.nf
  1829. X
  1830. X    %%
  1831. X    foo         return TOK_KEYWORD;
  1832. X    foobar      return TOK_KEYWORD;
  1833. X
  1834. X    fooba       |
  1835. X    foob        |
  1836. X    fo          {
  1837. X                /* false alarm, not really a keyword */
  1838. X                return TOK_ID;
  1839. X                }
  1840. X
  1841. X.fi
  1842. X.LP
  1843. XEliminating backtracking among a list of keywords can also be
  1844. Xdone using a "catch-all" rule:
  1845. X.nf
  1846. X
  1847. X    %%
  1848. X    foo         return TOK_KEYWORD;
  1849. X    foobar      return TOK_KEYWORD;
  1850. X
  1851. X    [a-z]+      return TOK_ID;
  1852. X
  1853. X.fi
  1854. XThis is usually the best solution when appropriate.
  1855. X.LP
  1856. XBacktracking messages tend to cascade.
  1857. XWith a complicated set of rules it's not uncommon to get hundreds
  1858. Xof messages.  If one can decipher them, though, it often
  1859. Xonly takes a dozen or so rules to eliminate the backtracking (though
  1860. Xit's easy to make a mistake and have an error rule accidentally match
  1861. Xa valid token.  A possible future
  1862. X.I flex
  1863. Xfeature will be to automatically add rules to eliminate backtracking).
  1864. X.LP
  1865. X.I Variable
  1866. Xtrailing context (where both the leading and trailing parts do not have
  1867. Xa fixed length) entails almost the same performance loss as
  1868. X.I REJECT
  1869. X(i.e., substantial).  So when possible a rule like:
  1870. X.nf
  1871. X
  1872. X    %%
  1873. X    mouse|rat/(cat|dog)   run();
  1874. X
  1875. X.fi
  1876. Xis better written:
  1877. X.nf
  1878. X
  1879. X    %%
  1880. X    mouse/cat|dog         run();
  1881. X    rat/cat|dog           run();
  1882. X
  1883. X.fi
  1884. Xor as
  1885. X.nf
  1886. X
  1887. X    %%
  1888. X    mouse|rat/cat         run();
  1889. X    mouse|rat/dog         run();
  1890. X
  1891. X.fi
  1892. XNote that here the special '|' action does
  1893. X.I not
  1894. Xprovide any savings, and can even make things worse (see
  1895. X.B BUGS
  1896. Xin flex(1)).
  1897. X.LP
  1898. XAnother area where the user can increase a scanner's performance
  1899. X(and one that's easier to implement) arises from the fact that
  1900. Xthe longer the tokens matched, the faster the scanner will run.
  1901. XThis is because with long tokens the processing of most input
  1902. Xcharacters takes place in the (short) inner scanning loop, and
  1903. Xdoes not often have to go through the additional work of setting up
  1904. Xthe scanning environment (e.g.,
  1905. X.B yytext)
  1906. Xfor the action.  Recall the scanner for C comments:
  1907. X.nf
  1908. X
  1909. X    %x comment
  1910. X    %%
  1911. X            int line_num = 1;
  1912. X
  1913. X    "/*"         BEGIN(comment);
  1914. X
  1915. X    <comment>[^*\\n]*
  1916. X    <comment>"*"+[^*/\\n]*
  1917. X    <comment>\\n             ++line_num;
  1918. X    <comment>"*"+"/"        BEGIN(INITIAL);
  1919. X
  1920. X.fi
  1921. XThis could be sped up by writing it as:
  1922. X.nf
  1923. X
  1924. X    %x comment
  1925. X    %%
  1926. X            int line_num = 1;
  1927. X
  1928. X    "/*"         BEGIN(comment);
  1929. X
  1930. X    <comment>[^*\\n]*
  1931. X    <comment>[^*\\n]*\\n      ++line_num;
  1932. X    <comment>"*"+[^*/\\n]*
  1933. X    <comment>"*"+[^*/\\n]*\\n ++line_num;
  1934. X    <comment>"*"+"/"        BEGIN(INITIAL);
  1935. X
  1936. X.fi
  1937. XNow instead of each newline requiring the processing of another
  1938. Xaction, recognizing the newlines is "distributed" over the other rules
  1939. Xto keep the matched text as long as possible.  Note that
  1940. X.I adding
  1941. Xrules does
  1942. X.I not
  1943. Xslow down the scanner!  The speed of the scanner is independent
  1944. Xof the number of rules or (modulo the considerations given at the
  1945. Xbeginning of this section) how complicated the rules are with
  1946. Xregard to operators such as '*' and '|'.
  1947. X.LP
  1948. XA final example in speeding up a scanner: suppose you want to scan
  1949. Xthrough a file containing identifiers and keywords, one per line
  1950. Xand with no other extraneous characters, and recognize all the
  1951. Xkeywords.  A natural first approach is:
  1952. X.nf
  1953. X
  1954. X    %%
  1955. X    asm      |
  1956. X    auto     |
  1957. X    break    |
  1958. X    ... etc ...
  1959. X    volatile |
  1960. X    while    /* it's a keyword */
  1961. X
  1962. X    .|\\n     /* it's not a keyword */
  1963. X
  1964. X.fi
  1965. XTo eliminate the back-tracking, introduce a catch-all rule:
  1966. X.nf
  1967. X
  1968. X    %%
  1969. X    asm      |
  1970. X    auto     |
  1971. X    break    |
  1972. X    ... etc ...
  1973. X    volatile |
  1974. X    while    /* it's a keyword */
  1975. X
  1976. X    [a-z]+   |
  1977. X    .|\\n     /* it's not a keyword */
  1978. X
  1979. X.fi
  1980. XNow, if it's guaranteed that there's exactly one word per line,
  1981. Xthen we can reduce the total number of matches by a half by
  1982. Xmerging in the recognition of newlines with that of the other
  1983. Xtokens:
  1984. X.nf
  1985. X
  1986. X    %%
  1987. X    asm\\n    |
  1988. X    auto\\n   |
  1989. X    break\\n  |
  1990. X    ... etc ...
  1991. X    volatile\\n |
  1992. X    while\\n  /* it's a keyword */
  1993. X
  1994. X    [a-z]+\\n |
  1995. X    .|\\n     /* it's not a keyword */
  1996. X
  1997. X.fi
  1998. XOne has to be careful here, as we have now reintroduced backtracking
  1999. Xinto the scanner.  In particular, while
  2000. X.I we
  2001. Xknow that there will never be any characters in the input stream
  2002. Xother than letters or newlines,
  2003. X.I flex
  2004. Xcan't figure this out, and it will plan for possibly needing backtracking
  2005. Xwhen it has scanned a token like "auto" and then the next character
  2006. Xis something other than a newline or a letter.  Previously it would
  2007. Xthen just match the "auto" rule and be done, but now it has no "auto"
  2008. Xrule, only a "auto\\n" rule.  To eliminate the possibility of backtracking,
  2009. Xwe could either duplicate all rules but without final newlines, or,
  2010. Xsince we never expect to encounter such an input and therefore don't
  2011. Xhow it's classified, we can introduce one more catch-all rule, this
  2012. Xone which doesn't include a newline:
  2013. X.nf
  2014. X
  2015. X    %%
  2016. X    asm\\n    |
  2017. X    auto\\n   |
  2018. X    break\\n  |
  2019. X    ... etc ...
  2020. X    volatile\\n |
  2021. X    while\\n  /* it's a keyword */
  2022. X
  2023. X    [a-z]+\\n |
  2024. X    [a-z]+   |
  2025. X    .|\\n     /* it's not a keyword */
  2026. X
  2027. X.fi
  2028. XCompiled with
  2029. X.B -Cf,
  2030. Xthis is about as fast as one can get a
  2031. X.I flex 
  2032. Xscanner to go for this particular problem.
  2033. X.LP
  2034. XA final note:
  2035. X.I flex
  2036. Xis slow when matching NUL's, particularly when a token contains
  2037. Xmultiple NUL's.
  2038. XIt's best to write rules which match
  2039. X.I short
  2040. Xamounts of text if it's anticipated that the text will often include NUL's.
  2041. X.SH INCOMPATIBILITIES WITH LEX AND POSIX
  2042. X.I flex
  2043. Xis a rewrite of the Unix
  2044. X.I lex
  2045. Xtool (the two implementations do not share any code, though),
  2046. Xwith some extensions and incompatibilities, both of which
  2047. Xare of concern to those who wish to write scanners acceptable
  2048. Xto either implementation.  At present, the POSIX
  2049. X.I lex
  2050. Xdraft is
  2051. Xvery close to the original
  2052. X.I lex
  2053. Ximplementation, so some of these
  2054. Xincompatibilities are also in conflict with the POSIX draft.  But
  2055. Xthe intent is that except as noted below,
  2056. X.I flex
  2057. Xas it presently stands will
  2058. Xultimately be POSIX conformant (i.e., that those areas of conflict with
  2059. Xthe POSIX draft will be resolved in
  2060. X.I flex's
  2061. Xfavor).  Please bear in
  2062. Xmind that all the comments which follow are with regard to the POSIX
  2063. X.I draft
  2064. Xstandard of Summer 1989, and not the final document (or subsequent
  2065. Xdrafts); they are included so
  2066. X.I flex
  2067. Xusers can be aware of the standardization issues and those areas where
  2068. X.I flex
  2069. Xmay in the near future undergo changes incompatible with
  2070. Xits current definition.
  2071. X.LP
  2072. X.I flex
  2073. Xis fully compatible with
  2074. X.I lex
  2075. Xwith the following exceptions:
  2076. X.IP -
  2077. XThe undocumented
  2078. X.I lex
  2079. Xscanner internal variable
  2080. X.B yylineno
  2081. Xis not supported.  It is difficult to support this option efficiently,
  2082. Xsince it requires examining every character scanned and reexamining
  2083. Xthe characters when the scanner backs up.
  2084. XThings get more complicated when the end of buffer or file is reached or a
  2085. XNUL is scanned (since the scan must then be restarted with the proper line
  2086. Xnumber count), or the user uses the yyless(), unput(), or REJECT actions,
  2087. Xor the multiple input buffer functions.
  2088. X.IP
  2089. XThe fix is to add rules which, upon seeing a newline, increment
  2090. Xyylineno.  This is usually an easy process, though it can be a drag if some
  2091. Xof the patterns can match multiple newlines along with other characters.
  2092. X.IP
  2093. Xyylineno is not part of the POSIX draft.
  2094. X.IP -
  2095. XThe
  2096. X.B input()
  2097. Xroutine is not redefinable, though it may be called to read characters
  2098. Xfollowing whatever has been matched by a rule.  If
  2099. X.B input()
  2100. Xencounters an end-of-file the normal
  2101. X.B yywrap()
  2102. Xprocessing is done.  A ``real'' end-of-file is returned by
  2103. X.B input()
  2104. Xas
  2105. X.I EOF.
  2106. X.IP
  2107. XInput is instead controlled by redefining the
  2108. X.B YY_INPUT
  2109. Xmacro.
  2110. X.IP
  2111. XThe
  2112. X.I flex
  2113. Xrestriction that
  2114. X.B input()
  2115. Xcannot be redefined is in accordance with the POSIX draft, but
  2116. X.B YY_INPUT
  2117. Xhas not yet been accepted into the draft (and probably won't; it looks
  2118. Xlike the draft will simply not specify any way of controlling the
  2119. Xscanner's input other than by making an initial assignment to
  2120. X.I yyin).
  2121. X.IP -
  2122. X.I flex
  2123. Xscanners do not use stdio for input.  Because of this, when writing an
  2124. Xinteractive scanner one must explicitly call fflush() on the
  2125. Xstream associated with the terminal after writing out a prompt.
  2126. XWith
  2127. X.I lex
  2128. Xsuch writes are automatically flushed since
  2129. X.I lex
  2130. Xscanners use
  2131. X.B getchar()
  2132. Xfor their input.  Also, when writing interactive scanners with
  2133. X.I flex,
  2134. Xthe
  2135. X.B -I
  2136. Xflag must be used.
  2137. X.IP -
  2138. X.I flex
  2139. Xscanners are not as reentrant as
  2140. X.I lex
  2141. Xscanners.  In particular, if you have an interactive scanner and
  2142. Xan interrupt handler which long-jumps out of the scanner, and
  2143. Xthe scanner is subsequently called again, you may get the following
  2144. Xmessage:
  2145. X.nf
  2146. X
  2147. X    fatal flex scanner internal error--end of buffer missed
  2148. X
  2149. X.fi
  2150. XTo reenter the scanner, first use
  2151. X.nf
  2152. X
  2153. X    yyrestart( yyin );
  2154. X
  2155. X.fi
  2156. X.IP -
  2157. X.B output()
  2158. Xis not supported.
  2159. XOutput from the
  2160. X.B ECHO
  2161. Xmacro is done to the file-pointer
  2162. X.I yyout
  2163. X(default
  2164. X.I stdout).
  2165. X.IP
  2166. XThe POSIX draft mentions that an
  2167. X.B output()
  2168. Xroutine exists but currently gives no details as to what it does.
  2169. X.IP -
  2170. X.I lex
  2171. Xdoes not support exclusive start conditions (%x), though they
  2172. Xare in the current POSIX draft.
  2173. X.IP -
  2174. XWhen definitions are expanded,
  2175. X.I flex
  2176. Xencloses them in parentheses.
  2177. XWith lex, the following:
  2178. X.nf
  2179. X
  2180. X    NAME    [A-Z][A-Z0-9]*
  2181. X    %%
  2182. X    foo{NAME}?      printf( "Found it\\n" );
  2183. X    %%
  2184. X
  2185. X.fi
  2186. Xwill not match the string "foo" because when the macro
  2187. Xis expanded the rule is equivalent to "foo[A-Z][A-Z0-9]*?"
  2188. Xand the precedence is such that the '?' is associated with
  2189. X"[A-Z0-9]*".  With
  2190. X.I flex,
  2191. Xthe rule will be expanded to
  2192. X"foo([A-Z][A-Z0-9]*)?" and so the string "foo" will match.
  2193. XNote that because of this, the
  2194. X.B ^, $, <s>, /,
  2195. Xand
  2196. X.B <<EOF>>
  2197. Xoperators cannot be used in a
  2198. X.I flex
  2199. Xdefinition.
  2200. X.IP
  2201. XThe POSIX draft interpretation is the same as
  2202. X.I flex's.
  2203. X.IP -
  2204. XTo specify a character class which matches anything but a left bracket (']'),
  2205. Xin
  2206. X.I lex
  2207. Xone can use "[^]]" but with
  2208. X.I flex
  2209. Xone must use "[^\\]]".  The latter works with
  2210. X.I lex,
  2211. Xtoo.
  2212. X.IP -
  2213. XThe
  2214. X.I lex
  2215. X.B %r
  2216. X(generate a Ratfor scanner) option is not supported.  It is not part
  2217. Xof the POSIX draft.
  2218. X.IP -
  2219. XIf you are providing your own yywrap() routine, you must include a
  2220. X"#undef yywrap" in the definitions section (section 1).  Note that
  2221. Xthe "#undef" will have to be enclosed in %{}'s.
  2222. X.IP
  2223. XThe POSIX draft
  2224. Xspecifies that yywrap() is a function and this is very unlikely to change; so
  2225. X.I flex users are warned
  2226. Xthat
  2227. X.B yywrap()
  2228. Xis likely to be changed to a function in the near future.
  2229. X.IP -
  2230. XAfter a call to
  2231. X.B unput(),
  2232. X.I yytext
  2233. Xand
  2234. X.I yyleng
  2235. Xare undefined until the next token is matched.  This is not the case with
  2236. X.I lex
  2237. Xor the present POSIX draft.
  2238. X.IP -
  2239. XThe precedence of the
  2240. X.B {}
  2241. X(numeric range) operator is different.
  2242. X.I lex
  2243. Xinterprets "abc{1,3}" as "match one, two, or
  2244. Xthree occurrences of 'abc'", whereas
  2245. X.I flex
  2246. Xinterprets it as "match 'ab'
  2247. Xfollowed by one, two, or three occurrences of 'c'".  The latter is
  2248. Xin agreement with the current POSIX draft.
  2249. X.IP -
  2250. XThe precedence of the
  2251. X.B ^
  2252. Xoperator is different.
  2253. X.I lex
  2254. Xinterprets "^foo|bar" as "match either 'foo' at the beginning of a line,
  2255. Xor 'bar' anywhere", whereas
  2256. X.I flex
  2257. Xinterprets it as "match either 'foo' or 'bar' if they come at the beginning
  2258. Xof a line".  The latter is in agreement with the current POSIX draft.
  2259. X.IP -
  2260. XTo refer to yytext outside of the scanner source file,
  2261. Xthe correct definition with
  2262. X.I flex
  2263. Xis "extern char *yytext" rather than "extern char yytext[]".
  2264. XThis is contrary to the current POSIX draft but a point on which
  2265. X.I flex
  2266. Xwill not be changing, as the array representation entails a
  2267. Xserious performance penalty.  It is hoped that the POSIX draft will
  2268. Xbe emended to support the
  2269. X.I flex
  2270. Xvariety of declaration (as this is a fairly painless change to
  2271. Xrequire of
  2272. X.I lex
  2273. Xusers).
  2274. X.IP -
  2275. X.I yyin
  2276. Xis
  2277. X.I initialized
  2278. Xby
  2279. X.I lex
  2280. Xto be
  2281. X.I stdin;
  2282. X.I flex,
  2283. Xon the other hand,
  2284. Xinitializes
  2285. X.I yyin
  2286. Xto NULL
  2287. Xand then
  2288. X.I assigns
  2289. Xit to
  2290. X.I stdin
  2291. Xthe first time the scanner is called, providing
  2292. X.I yyin
  2293. Xhas not already been assigned to a non-NULL value.  The difference is
  2294. Xsubtle, but the net effect is that with
  2295. X.I flex
  2296. Xscanners,
  2297. X.I yyin
  2298. Xdoes not have a valid value until the scanner has been called.
  2299. X.IP -
  2300. XThe special table-size declarations such as
  2301. X.B %a
  2302. Xsupported by
  2303. X.I lex
  2304. Xare not required by
  2305. X.I flex
  2306. Xscanners;
  2307. X.I flex
  2308. Xignores them.
  2309. X.IP -
  2310. XThe name
  2311. X.bd
  2312. XFLEX_SCANNER
  2313. Xis #define'd so scanners may be written for use with either
  2314. X.I flex
  2315. Xor
  2316. X.I lex.
  2317. X.LP
  2318. XThe following
  2319. X.I flex
  2320. Xfeatures are not included in
  2321. X.I lex
  2322. Xor the POSIX draft standard:
  2323. X.nf
  2324. X
  2325. X    yyterminate()
  2326. X    <<EOF>>
  2327. X    YY_DECL
  2328. X    #line directives
  2329. X    %{}'s around actions
  2330. X    yyrestart()
  2331. X    comments beginning with '#' (deprecated)
  2332. X    multiple actions on a line
  2333. X
  2334. X.fi
  2335. XThis last feature refers to the fact that with
  2336. X.I flex
  2337. Xyou can put multiple actions on the same line, separated with
  2338. Xsemi-colons, while with
  2339. X.I lex,
  2340. Xthe following
  2341. X.nf
  2342. X
  2343. X    foo    handle_foo(); ++num_foos_seen;
  2344. X
  2345. X.fi
  2346. Xis (rather surprisingly) truncated to
  2347. X.nf
  2348. X
  2349. X    foo    handle_foo();
  2350. X
  2351. X.fi
  2352. X.I flex
  2353. Xdoes not truncate the action.  Actions that are not enclosed in
  2354. Xbraces are simply terminated at the end of the line.
  2355. X.SH DIAGNOSTICS
  2356. X.I reject_used_but_not_detected undefined
  2357. Xor
  2358. X.I yymore_used_but_not_detected undefined -
  2359. XThese errors can occur at compile time.  They indicate that the
  2360. Xscanner uses
  2361. X.B REJECT
  2362. Xor
  2363. X.B yymore()
  2364. Xbut that
  2365. X.I flex
  2366. Xfailed to notice the fact, meaning that
  2367. X.I flex
  2368. Xscanned the first two sections looking for occurrences of these actions
  2369. Xand failed to find any, but somehow you snuck some in (via a #include
  2370. Xfile, for example).  Make an explicit reference to the action in your
  2371. X.I flex
  2372. Xinput file.  (Note that previously
  2373. X.I flex
  2374. Xsupported a
  2375. X.B %used/%unused
  2376. Xmechanism for dealing with this problem; this feature is still supported
  2377. Xbut now deprecated, and will go away soon unless the author hears from
  2378. Xpeople who can argue compellingly that they need it.)
  2379. X.LP
  2380. X.I flex scanner jammed -
  2381. Xa scanner compiled with
  2382. X.B -s
  2383. Xhas encountered an input string which wasn't matched by
  2384. Xany of its rules.
  2385. X.LP
  2386. X.I flex input buffer overflowed -
  2387. Xa scanner rule matched a string long enough to overflow the
  2388. Xscanner's internal input buffer (16K bytes by default - controlled by
  2389. X.B YY_BUF_SIZE
  2390. Xin "flex.skel".  Note that to redefine this macro, you must first
  2391. X.B #undefine
  2392. Xit).
  2393. X.LP
  2394. X.I scanner requires -8 flag -
  2395. XYour scanner specification includes recognizing 8-bit characters and
  2396. Xyou did not specify the -8 flag (and your site has not installed flex
  2397. Xwith -8 as the default).
  2398. X.LP
  2399. X.I
  2400. Xfatal flex scanner internal error--end of buffer missed -
  2401. XThis can occur in an scanner which is reentered after a long-jump
  2402. Xhas jumped out (or over) the scanner's activation frame.  Before
  2403. Xreentering the scanner, use:
  2404. X.nf
  2405. X
  2406. X    yyrestart( yyin );
  2407. X
  2408. X.fi
  2409. X.LP
  2410. X.I too many %t classes! -
  2411. XYou managed to put every single character into its own %t class.
  2412. X.I flex
  2413. Xrequires that at least one of the classes share characters.
  2414. X.SH DEFICIENCIES / BUGS
  2415. XSee flex(1).
  2416. X.SH "SEE ALSO"
  2417. X.LP
  2418. Xflex(1), lex(1), yacc(1), sed(1), awk(1).
  2419. X.LP
  2420. XM. E. Lesk and E. Schmidt,
  2421. X.I LEX - Lexical Analyzer Generator
  2422. X.SH AUTHOR
  2423. XVern Paxson, with the help of many ideas and much inspiration from
  2424. XVan Jacobson.  Original version by Jef Poskanzer.  The fast table
  2425. Xrepresentation is a partial implementation of a design done by Van
  2426. XJacobson.  The implementation was done by Kevin Gong and Vern Paxson.
  2427. X.LP
  2428. XThanks to the many
  2429. X.I flex
  2430. Xbeta-testers, feedbackers, and contributors, especially Casey
  2431. XLeedom, benson@odi.com, Keith Bostic,
  2432. XFrederic Brehm, Nick Christopher, Jason Coughlin,
  2433. XScott David Daniels, Leo Eskin,
  2434. XChris Faylor, Eric Goldman, Eric
  2435. XHughes, Jeffrey R. Jones, Kevin B. Kenny, Ronald Lamprecht,
  2436. XGreg Lee, Craig Leres, Mohamed el Lozy, Jim Meyering, Marc Nozell, Esmond Pitt,
  2437. XJef Poskanzer, Jim Roskind,
  2438. XDave Tallman, Frank Whaley, Ken Yap, and those whose names
  2439. Xhave slipped my marginal mail-archiving skills but whose contributions
  2440. Xare appreciated all the same.
  2441. X.LP
  2442. XThanks to Keith Bostic, John Gilmore, Craig Leres, Bob
  2443. XMulcahy, Rich Salz, and Richard Stallman for help with various distribution
  2444. Xheadaches.
  2445. X.LP
  2446. XThanks to Esmond Pitt and Earle Horton for 8-bit character support;
  2447. Xto Benson Margulies and Fred
  2448. XBurke for C++ support; to Ove Ewerlid for the basics of support for
  2449. XNUL's; and to Eric Hughes for the basics of support for multiple buffers.
  2450. X.LP
  2451. XWork is being done on extending
  2452. X.I flex
  2453. Xto generate scanners in which the
  2454. Xstate machine is directly represented in C code rather than tables.
  2455. XThese scanners may well be substantially faster than those generated
  2456. Xusing -f or -F.  If you are working in this area and are interested
  2457. Xin comparing notes and seeing whether redundant work can be avoided,
  2458. Xcontact Ove Ewerlid (ewerlid@mizar.DoCS.UU.SE).
  2459. X.LP
  2460. XThis work was primarily done when I was at the Real Time Systems Group
  2461. Xat the Lawrence Berkeley Laboratory in Berkeley, CA.  Many thanks to all there
  2462. Xfor the support I received.
  2463. X.LP
  2464. XSend comments to:
  2465. X.nf
  2466. X
  2467. X     Vern Paxson
  2468. X     Computer Science Department
  2469. X     4126 Upson Hall
  2470. X     Cornell University
  2471. X     Ithaca, NY 14853-7501
  2472. X
  2473. X     vern@cs.cornell.edu
  2474. X     decvax!cornell!vern
  2475. X
  2476. X.fi
  2477. END_OF_FILE
  2478. if test 65353 -ne `wc -c <'flexdoc.1'`; then
  2479.     echo shar: \"'flexdoc.1'\" unpacked with wrong size!
  2480. fi
  2481. # end of 'flexdoc.1'
  2482. fi
  2483. echo shar: End of archive 11 \(of 13\).
  2484. cp /dev/null ark11isdone
  2485. MISSING=""
  2486. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 ; do
  2487.     if test ! -f ark${I}isdone ; then
  2488.     MISSING="${MISSING} ${I}"
  2489.     fi
  2490. done
  2491. if test "${MISSING}" = "" ; then
  2492.     echo You have unpacked all 13 archives.
  2493.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2494. else
  2495.     echo You still need to unpack the following archives:
  2496.     echo "        " ${MISSING}
  2497. fi
  2498. ##  End of shell archive.
  2499. exit 0
  2500. -- 
  2501. Mail submissions (sources or binaries) to <amiga@uunet.uu.net>.
  2502. Mail comments to the moderator at <amiga-request@uunet.uu.net>.
  2503. Post requests for sources, and general discussion to comp.sys.amiga.
  2504.